package com.breeze.leetcode;

/**
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
 * 122. 买卖股票的最佳时机 II
 * 给定一个数组，它的第 i 个元素是一支给定股票第 i 天的价格。
 * <p>
 * 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易（多次买卖一支股票）。
 * <p>
 * 注意：你不能同时参与多笔交易（你必须在再次购买前出售掉之前的股票）。
 */
public class LeetCode122 {
    /**
     *  TODO:
     */
    public int maxProfit(int[] prices) {
        int maxProfit = 0;
        int minPrice = Integer.MAX_VALUE;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] <= minPrice) {
                minPrice = prices[i];
            } else {
                if (((i < prices.length - 1 && prices[i] > prices[i + 1]) || i == prices.length - 1) && prices[i] > minPrice) {
                    maxProfit += prices[i] - minPrice;
                    minPrice = Integer.MAX_VALUE;
                }
            }
        }
        return maxProfit;
    }

    /**
     * TODO:
     */
    public int maxProfit1(int[] prices) {
        int maxProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                maxProfit += prices[i] - prices[i - 1];
            }
        }
        return maxProfit;
    }


    public static void main(String[] args) {
        LeetCode122 leetCode122 = new LeetCode122();
        int[] prices1 = {7, 1, 5, 3, 6, 4}; // 7
        int[] prices2 = {7, 6, 4, 3, 1}; //0
        int[] prices3 = {1, 2, 3, 4, 5}; //4
        int[] prices4 = {}; //0
        int maxProfit1 = leetCode122.maxProfit1(prices1);
        int maxProfit2 = leetCode122.maxProfit1(prices2);
        int maxProfit3 = leetCode122.maxProfit1(prices3);
        int maxProfit4 = leetCode122.maxProfit1(prices4);
        System.out.println(maxProfit1);
        System.out.println(maxProfit2);
        System.out.println(maxProfit3);
        System.out.println(maxProfit4);
    }
}
